<?php
/**
 * 用作个人中心
 */
namespace Content\Controller;

use Content\Model\ArticleModel;
use Home\Model\AdminUserModel;
use Think\Controller;
use Common\Controller\UBaseController;

class ArticleController extends UBaseController
{
    public function addp()
    {
        $this->assign("pageId","A003001");
        $this->_title("新增文章");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('add');
    }

    public function listp()
    {
        $p = $this->params['p'] ?: 1;
        $limit = 10;
        $offset = ($p - 1) * $limit;

        if (empty($this->params['type'])) {
            $con = "`type` in(1,2,3)";
        } else {
            $con = "`type`=" . intval($this->params['type']);
        }

        $order = [
            'id' => 'DESC'
        ];
        $model = new ArticleModel();
        $list = $model->where($con)->order($order)->limit($offset, $limit)->select();
        foreach ($list as $k=>$v) {
            unset($list[$k]['content']);
            $list[$k]['created'] = date("Y-m-d H:i", $v['created']);
            $list[$k]['publish_time'] = empty($v['publish_time']) ? "未设置" : date("Y-m-d H:i", $v['publish_time']);
            $list[$k]['status_text'] = $v['status'] == 1 ? "显示" : "隐藏";
            $list[$k]['type_text'] = ArticleModel::$typeConfig[$v['type']];
        }

        $this->quickBuildForm();
        $this->assign("list", $list);
        //配置当前页面
        $this->assign("pageId", "A003002");
        //设置页面标题,非必须
        $this->_title("文章列表");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('list');
    }

    public function about()
    {
        $model = new ArticleModel();
        $article = $model->getArticleByType(4);
        $this->assign('data', $article);
        layout("default");
        $this->assign('pageId', 'A003003');
        $this->display("about");
    }

    public function edit()
    {
        $id = empty($this->params['id']) ? 0 : (int)$this->params['id'];
        $model = new ArticleModel();
        $data = $model->getArticleById($id);
        $this->assign("data", $data);
        layout("default");
        $this->assign("pageId", "A003002");
        $this->display("edit");
    }
    /**
     * 文章保存,新增&编辑ajax
     */
    public function save()
    {
        if (empty($this->params['title'])
        || empty($this->params['content'])
        || empty($this->params['type'])) {
            $this->jsonReturn(10000,"","参数不全");
        }

        $model = new ArticleModel();

        $data = [
            'type'  => intval($this->params['type']),
            'title' => trim($this->params['title']),
            'content'   => trim($this->params['content']),
            'pre_content'   => trim($this->params['pre_content']),
            'headimage' => empty($this->params['headimage']) ? null : trim($this->params['headimage']),
        ];
        if (!empty($this->params['publish_time'])) {
            $data['publish_time'] = strtotime($this->params['publish_time']);
        }

        if (empty($this->params['id'])) {
            //新增文章
            $data['status']     = 0;
            $data['index_show'] = 0;
            $data['index_time'] = 0;
            $data['created']    = time();

            $articleId = $model->add($data);
        } else {
            $res = $model->where(['id' => intval($this->params['id'])])->data($data)->save();
            $articleId = $res ? $this->params['id'] : false;
        }
        if ($articleId) {
            $this->jsonReturn(200,['article_id' => intval($articleId)]);
        } else {
            $this->jsonReturn(10001,"", "保存失败,请重试!");
        }
    }

    /**
     * ajax请求
     */
    public function ajax()
    {
        if (empty($this->params['action'])) {
            $this->jsonReturn(10000,"","参数不全");
        }

        switch ($this->params['action']) {
            case 'publish': //发布/取消发布
                $model = new ArticleModel();
                $id = empty($this->params['id']) ? 0 : $this->params['id'];
                $res = $model->publish($id);
                $this->jsonReturn($res['code'],"",$res["msg"]);
                break;
            case 'index'://上首页
                $model = new ArticleModel();
                $id = empty($this->params['id']) ? 0 : $this->params['id'];
                $res = $model->pushIndex($id);
                $this->jsonReturn($res['code'],"",$res["msg"]);
                break;
            default:
                $this->jsonReturn(10000,"","参数不全");
                break;
        }

        exit;
    }
}